package com.dtw.ditouwang.ui.mine;

import android.app.AlertDialog;
import android.arch.lifecycle.Observer;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.dtw.ditouwang.R;
import com.dtw.ditouwang.beans.CommonBean;
import com.dtw.ditouwang.beans.UserBean;
import com.dtw.ditouwang.beans.WechatUserBean;
import com.dtw.ditouwang.repositories.UserRepository;
import com.dtw.ditouwang.ui.BaseActivity;
import com.dtw.ditouwang.ui.login.LoginActivity;
import com.dtw.ditouwang.ui.main.MainActivity;
import com.dtw.ditouwang.utils.SharedPreferencesUtil;

import cn.jiguang.share.android.api.AuthListener;
import cn.jiguang.share.android.api.JShareInterface;
import cn.jiguang.share.android.api.Platform;
import cn.jiguang.share.android.model.AccessTokenInfo;
import cn.jiguang.share.android.model.BaseResponseInfo;
import cn.jiguang.share.wechat.Wechat;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class BindAccountActivity extends BaseActivity implements View.OnClickListener {
    TextView  phone,weichat,weibo;
    View bindPhone,bindWechat,bindWeibo;
    String codestr = "-1";
    TextView getcode;
    CountDownTimer countDownTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_account);
//        enableBack();
//        setMainTitle("账号与安全");
        phone = findViewById(R.id.text_phone);
        weichat = findViewById(R.id.text_wechat);
        weibo = findViewById(R.id.text_weibo);
        bindPhone = findViewById(R.id.view_bing_phone);
        bindWechat = findViewById(R.id.view_bing_wechat);
        bindWeibo = findViewById(R.id.view_bing_weibo);
        bindPhone.setOnClickListener(this);
        bindWechat.setOnClickListener(this);
        bindWeibo.setOnClickListener(this);

        countDownTimer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                getcode.setText(millisUntilFinished / 1000 + " s");
            }

            @Override
            public void onFinish() {
                getcode.setText("获取验证码");
                getcode.setEnabled(true);
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        phone.setText(UserRepository.getInstance().getUserBean().getData().getUsername());
        weichat.setText(UserRepository.getInstance().getUserBean().getData().getWx_nickname());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_bing_phone:
                final EditText editText=new EditText(this);
                editText.setEms(10);
                editText.setInputType(InputType.TYPE_CLASS_PHONE);
                new AlertDialog.Builder(this).setView(editText).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        webApi.bindPhone(UserRepository.getInstance().getUserID(),editText.getText().toString()).enqueue(new Callback<CommonBean>() {
                            @Override
                            public void onResponse(Call<CommonBean> call, Response<CommonBean> response) {
                                if(response.body()!=null&&response.body().isResult()){
                                    UserBean userBean=UserRepository.getInstance().getUserBean();
                                    userBean.getData().setUsername(editText.getText().toString());
                                    showToast("绑定手机号成功");
                                    onResume();
                                }
                            }

                            @Override
                            public void onFailure(Call<CommonBean> call, Throwable t) {

                            }
                        });
                    }
                }).show();
                break;
            case R.id.view_bing_wechat:
                JShareInterface.authorize(Wechat.Name, new AuthListener() {
                    @Override
                    public void onComplete(Platform platform, int i, BaseResponseInfo baseResponseInfo) {
                        webApi.getWechatUserInfo(((AccessTokenInfo) baseResponseInfo).getToken(),((AccessTokenInfo) baseResponseInfo).getOpenid()).enqueue(new Callback<WechatUserBean>() {
                            @Override
                            public void onResponse(Call<WechatUserBean> call, final Response<WechatUserBean> response) {
                                if(response.body()!=null) {
                                    webApi.bindWeichat(UserRepository.getInstance().getUserID(), response.body().getNickname(), response.body().getOpenid()).enqueue(new Callback<CommonBean>() {
                                        @Override
                                        public void onResponse(Call<CommonBean> call, Response<CommonBean> response2) {
                                            if (response2.body() != null && response2.body().isResult()) {
                                                UserBean userBean = UserRepository.getInstance().getUserBean();
                                                userBean.getData().setWx_id(response.body().getOpenid());
                                                userBean.getData().setWx_nickname(response.body().getNickname());
                                                showToast("绑定微信成功");
                                                onResume();
                                            }
                                        }

                                        @Override
                                        public void onFailure(Call<CommonBean> call, Throwable t) {

                                        }
                                    });
                                }
                            }

                            @Override
                            public void onFailure(Call<WechatUserBean> call, Throwable t) {
                                showToast("登录失败");
                                finish();
                            }
                        });
                    }

                    @Override
                    public void onError(Platform platform, int i, int i1, Throwable throwable) {
                        Log.i("dtw", "onError:" + platform);

                    }

                    @Override
                    public void onCancel(Platform platform, int i) {
                        Log.i("dtw", "onCancel:" + platform);

                    }
                });
                break;
            case R.id.view_bing_weibo:

                break;
        }
    }
}
